package cn.wit.luohui.day09;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javafx.scene.chart.Axis;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;

public class ChartUtils {

	// 创建一个表格
	public static LineChart<String, Number> createLineChart(String title, Map<String, Number> scores) {
		// 创建X轴和Y轴
		Axis<String> xAxis = new CategoryAxis();
		Axis<Number> yAxis = new NumberAxis();
		yAxis.setLabel("分数");
		xAxis.setLabel("姓名");
		
		LineChart<String, Number> lineChart = new LineChart<>(xAxis, yAxis);
		
		Series<String, Number> series = new Series<>();
		// 添加每一个坐标点
		
		for (Entry<String, Number> entry : scores.entrySet()) {
			series.getData().add(new Data<String, Number>(entry.getKey(), entry.getValue()));
		}
		
		series.setName(title);
		
		lineChart.getData().add(series);
		return lineChart;
	}
	
//	// 同时创建多个表格
	public static List<LineChart<String, Number>> createLineCharts(
			// String title1, Map<String, Number> scores1,
			// String title2, Map<String, Number> scores2
			Map<String, Map<String, Number>> map) {

		List<LineChart<String, Number>> charts = new ArrayList<>();

		for (Entry<String, Map<String, Number>> entry : map.entrySet()) {
			String title = entry.getKey(); // 语文成绩
			Map<String, Number> scores = entry.getValue();

			// 创建某个课程对应的表格
			LineChart<String, Number> lineChart = createLineChart(title, scores);
			charts.add(lineChart);
		}

		return charts;
	}
	
}
